package org.eclipse.soc.ide4edu.assignment.wizards;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Calendar;

import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class NewAssignmentWizardPage extends WizardPage {

	/*--- SWT Widgets ---*/
	private Text assignmentNameTextField;
	private Text assignmentPathTextField;
	private Button browse;
	private Button createSeparatePath;
	private Button createInWorkspace;
	private Text professorName;
	private Text departmentName;
	private Text universityName;
	private Text courseName;
	private Text courseCode;
	/*--- End ---*/
	
	private IPath projectImportPath = null;
	private String assignmentName = null;

	private InputStream contentStream;

	/**
	 * Constructor of this class.
	 * @param select ISelection element
	 */
	public NewAssignmentWizardPage(ISelection select) {
		super("TeacherWizardPage1");
		setTitle("New Assignment Wizard Page");
	}

	/**
	 * Checks if the Assignment Field is empty and sets setPageComplete()
	 * accordingly.
	 */
	protected void checkName() {

		boolean done = false;

		if (assignmentNameTextField.getText().length() == 0)
			setErrorMessage("Please enter a name");
		else {
			done = true;
			setErrorMessage(null);
		}

		setPageComplete(done);

	}

	@Override
	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout gl = new GridLayout();
		composite.setLayout(gl);
		gl.verticalSpacing = 10;

		new Label(composite, SWT.NONE).setText("Assignment Name: ");
		assignmentNameTextField = new Text(composite, SWT.BORDER | SWT.SINGLE);
		assignmentNameTextField.addModifyListener(new ModifyListener() {

			public void modifyText(ModifyEvent e) {
				checkName();

			}

		});
		GridData gd = new GridData(GridData.FILL_HORIZONTAL
				| GridData.GRAB_HORIZONTAL);

		gd.grabExcessHorizontalSpace = true;
		assignmentNameTextField.setLayoutData(gd);
		gd.horizontalSpan = 2;

		gl = new GridLayout();
		gd = new GridData(GridData.FILL_HORIZONTAL);
		Group contents = new Group(composite, SWT.SHADOW_IN);
		contents.setText("Contents");
		gl.numColumns = 3;

		contents.setLayout(gl);
		contents.setLayoutData(gd);

		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.grabExcessHorizontalSpace = true;
		gd.horizontalSpan = 3;
		createInWorkspace = new Button(contents, SWT.RADIO);
		createInWorkspace.setText("Create a New Assignment in Workspace");
		createInWorkspace.setEnabled(true);
		createInWorkspace.setLayoutData(gd);

		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.grabExcessHorizontalSpace = true;
		gd.horizontalSpan = 3;
		createSeparatePath = new Button(contents, SWT.RADIO);
		createSeparatePath
				.setText("Create an Assignment in a Separate Directory");
		createSeparatePath.setLayoutData(gd);

		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.grabExcessHorizontalSpace = true;
		new Label(contents, SWT.BEGINNING).setText("Path:");
		assignmentPathTextField = new Text(contents, SWT.BORDER | SWT.SINGLE);
		gd.widthHint = 30;
		assignmentPathTextField.setLayoutData(gd);
		browse = new Button(contents, SWT.PUSH);
		browse.setText("Browse..");
		browse.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				handleBrowse();
			}
		});

		gl = new GridLayout();
		gd = new GridData(GridData.FILL_HORIZONTAL);
		Group assignmentInfo = new Group(composite, SWT.SHADOW_IN);
		assignmentInfo.setText("Assignment Information");
		gl.numColumns = 2;
		gd.horizontalAlignment = SWT.FILL;
		assignmentInfo.setLayout(gl);
		assignmentInfo.setLayoutData(gd);

		new Label(assignmentInfo, SWT.NONE).setText("Professor Name:");
		professorName = new Text(assignmentInfo, SWT.BORDER | SWT.SINGLE);
		professorName.setLayoutData(gd);

		new Label(assignmentInfo, SWT.NONE).setText("Course Name:");
		courseName = new Text(assignmentInfo, SWT.BORDER | SWT.SINGLE);
		courseName.setLayoutData(gd);

		new Label(assignmentInfo, SWT.NONE).setText("Course Code:");
		courseCode = new Text(assignmentInfo, SWT.BORDER | SWT.SINGLE);
		courseCode.setLayoutData(gd);

		new Label(assignmentInfo, SWT.NONE).setText("Department:");
		departmentName = new Text(assignmentInfo, SWT.BORDER | SWT.SINGLE);
		departmentName.setLayoutData(gd);

		new Label(assignmentInfo, SWT.NONE).setText("University Name:");
		universityName = new Text(assignmentInfo, SWT.BORDER | SWT.SINGLE);
		universityName.setLayoutData(gd);

		new Label(assignmentInfo, SWT.NONE)
				.setText("Assignment Creation Date: ");
		new Label(assignmentInfo, SWT.NONE).setText(getCurrentTime());
		setControl(composite);

	}

	/**
	 * Helper Method to get the Assignment Details.
	 * 
	 * @return the InputStream of the collection of details of this WizardPage.
	 */
	public InputStream getAssignmentDetails() {
		String content = new String();
		content += "Assignment Name:" + getAssignmentName() + "\n";
		content += "Professor Name:" + getProfessorName() + "\n";
		content += "Course Name:" + getCourseName() + "\n";
		content += "Course Code:" + getCourseCode() + "\n";
		content += "Department Name:" + getDepartmentName() + "\n";
		content += "University Name:" + getUniversityName() + "\n";

		contentStream = new ByteArrayInputStream(content.getBytes());
		return contentStream;
	}

	/**
	 * Helper Method to get the Assignment's Name.
	 * 
	 * @return the String value of the Assignment's Name.
	 */
	public String getAssignmentName() {
		assignmentName = assignmentNameTextField.getText();

		return assignmentName;
	}

	/**
	 * Helper Method to get the Assignment's Path.
	 * 
	 * @return the String value of the Assignment's Path.
	 */
	public String getAssignmentPath() {

		return assignmentPathTextField.getText();

	}

	/**
	 * Helper Method to get the Course Code.
	 * 
	 * @return the String value of the Course Code.
	 */
	public String getCourseCode() {
		return courseCode.getText();
	}

	/**
	 * Helper Method to get the Course Name.
	 * 
	 * @return the String value of the Course Name.
	 */
	public String getCourseName() {
		return courseName.getText();
	}

	/**
	 * Helper Method to get the Current System Time.
	 * 
	 * @return the String representation of the Current System Time.
	 */
	private String getCurrentTime() {
		Calendar time = Calendar.getInstance();
		return time.getTime().toString();
	}

	/**
	 * Helper Method to get the Department's Name.
	 * 
	 * @return the String value of the Department's Name.
	 */
	public String getDepartmentName() {
		return departmentName.getText();
	}

	/**
	 * Helper Method to get the Professor's Name.
	 * 
	 * @return the String value of the Professor's Name.
	 */
	public String getProfessorName() {
		return professorName.getText();
	}

	/**
	 * Helper Method to get the Univeristy's Name.
	 * 
	 * @return the String value of the University's Name.
	 */
	public String getUniversityName() {
		return universityName.getText();
	}

	/**
     * Opens the DirectoryDialog and sets the assignmentPathTextField 
     */
	public void handleBrowse() {
		DirectoryDialog dialog = new DirectoryDialog(getShell());
		assignmentPathTextField.setText(dialog.open());
	}

}
